<template>
	<view class="container">
		<!-- 搜索栏 -->
		<view class="search-box animated-item" style="animation-delay: 0.1s">
			<u-search placeholder="搜索新闻" v-model="keyword" bgColor="#f5f7fa" @seach="handleSearch"
				@custom="handleSearch"></u-search>
		</view>

		<!-- 新闻列表 -->
		<view class="news-list">
			<uni-card 
				style="margin-bottom: 70rpx; border-radius: 30rpx;" 
				v-for="(item, index) in newsList" 
				:key="index"
				margin="15rpx 0" 
				@click="openNews(item.newsId)"
				class="animated-item"
				:style="{animationDelay: `${0.2 + index*0.1}s`}"
			>
				<view class="news-content">
					<image v-if="item.cover_image" class="news-cover" :src="item.cover_image" mode="aspectFill"></image>
					<text class="news-title">{{ item.title }}</text>
				</view>

				<view slot="actions" class="card-actions">
					<view class="meta-item">
						<u-icon name="account" size="16" color="#909399"></u-icon>
						<text class="meta-text">{{ item.createBy }}</text>
					</view>
					<view class="meta-item">
						<u-icon name="clock" size="16" color="#909399"></u-icon>
						<text class="meta-text">{{ formatDate(item.createTime) }}</text>
					</view>
				</view>
			</uni-card>
		</view>
	</view>
</template>

<script>
	import {
		listStudynews,
	} from '@/api/studytourism/studynews'
	export default {
		data() {
			return {
				keyword: '',
				newsList: []
			}
		},
		onLoad() {
			this.getNewsList();
		},
		methods: {
			handleSearch() {
				this.getNewsList(this.keyword)
			},
			async getNewsList(keyword = '') {
				const params = {
					pageNum: this.pageNum,
					pageSize: this.pageSize,
					title: keyword
				}
				const res = await listStudynews(params)
				if (res.code === 200) {
					this.newsList = res.rows.map(item => ({
						...item,
						cover_image: `http://localhost:8080${item.coverImage}`
					}))
				}
			},
			formatDate(dateStr) {
				if (!dateStr) return '';
				const date = new Date(dateStr);
				return `${date.getFullYear()}-${(date.getMonth()+1).toString().padStart(2, '0')}-${date.getDate().toString().padStart(2, '0')}`;
			},
			openNews(newsId) {
				uni.navigateTo({
					url: `/pages/index/news/newsDetail?newsId=${newsId}`
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.container {
		padding: 20rpx;
		background-color: #fff;
		min-height: 100vh;
	}

	.search-box {
		padding: 20rpx;
		background-color: #fff;
	}

	.news-list {
		margin-top: 50rpx;
		border-radius: 18rpx;
		.uni-card {
			margin-top: 50rpx;
		}
	}

	.news-content {
		display: flex;
		flex-direction: column;
	}

	.news-cover {
		width: 100%;
		height: 300rpx;
		border-radius: 8rpx;
		margin-bottom: 20rpx;
	}

	.news-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #303133;
		line-height: 1.5;
	}

	.card-actions {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 20rpx 0;
		border-top: 1rpx solid #ebeef5;
	}

	.meta-item {
		display: flex;
		align-items: center;
	}

	.meta-text {
		margin-left: 8rpx;
		font-size: 24rpx;
		color: #909399;
	}

	/* 新增动画样式 */
	.animated-item {
		opacity: 0;
		animation: fadeInUp 0.6s ease forwards;
	}

	@keyframes fadeInUp {
		from {
			opacity: 0;
			transform: translateY(20px);
		}
		to {
			opacity: 1;
			transform: translateY(0);
		}
	}
</style>